// patrolnpc.txt
// Creature attacks anything it hates nearby, and spends the rest of its time patrolling a path.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - Attack type.
//	 Cell 1 - If 0, just loops around path. If 1, walks to end of path, waits a little, and returns home.
//      If 2, goes to end of path and just stops.
//   Cell 2 - Node if talked to. 0 means no conversation
//   Cell 3,4 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

variables;

short i,target;
short last_abil;
short ae_range = 7;

body;

beginstate INIT_STATE;
	set_name(ME,"Power Construct");
	set_level(ME,28);
	set_boss_level(ME,2);
	set_walk_speed(ME,22);
	
	last_abil = get_current_tick();
	
	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(3) != 0) || (get_memory_cell(4) != 0))
		inc_flag(get_memory_cell(3),get_memory_cell(4),1);
break;

beginstate START_STATE;
	if ((is_combat()) && (get_attitude(ME) < 10)) {
		while (my_ap() > 6) {
			deduct_ap(1);
			}
		}
		
	if (get_foe_target(ME,6,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if ((gf(55,16) >= 4) && (get_attitude(ME) < 10)) {
		if (get_nearest_party_char(3) >= 0) {
			print_str_color("A shade has detected you! It swoops to attack.",2);
			set_attitude(ME,10);
			if (gf(55,17) == 0) {
				sf(55,17,1);
				begin_talk_mode(8);
				}
			}
		}
		
	if (get_memory_cell(1) == 0) 
		follow_path(ME,0,1);

	// repeat for safety
	if ((gf(55,16) >= 4) && (get_attitude(ME) < 10)) {
		if (get_nearest_party_char(3) >= 0) {
			print_str_color("A shade has detected you! It swoops to attack.",2);
			set_attitude(ME,10);
			if (gf(55,17) == 0) {
				sf(55,17,1);
				begin_talk_mode(8);
				}
			}
		}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((is_combat()) && 
	  (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();
	  	if (get_memory_cell(0) == 0) { // fire
		  	place_particle_num(ME,2,4,8);
			print_named_str(ME,"surrounds itself with a curtain of flame.");
			pc_heard_sound_delay(117,250);						
	  		create_missile_spiral(154,40,ae_range,2);
	  		damage_nearby(get_ran(1,250,300),ae_range,2,0);
	  		}

	  	if (get_memory_cell(0) == 1) { // charm
		  	place_particle_num(ME,0,6,8);
			print_named_str(ME,"emits a strange, wonderful scent.");
			pc_heard_sound_delay(100,250);						
			status_nearby(50,ae_range,5,0);
	  		}

	  	if (get_memory_cell(0) == 2) { // lightning aura
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"surrounds itself with an aura of lightning.");
			pc_heard_sound_delay(179,250);						
	  		create_missile_spiral(147,40,ae_range,2);
	  		damage_nearby(get_ran(1,150,200),ae_range,1,0);
			status_nearby(get_level(ME) / 4,ae_range,20,0);
	  		}


		end();
		}
		
	do_attack();
break;


beginstate TALKING_STATE;
		print_str("Talking: The shade doesn't respond.");
break;